package com.douqu.game.main.msg;


import com.douqu.game.core.e.E_ServerType;
import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.entity.ext.BattleInfo;
import com.douqu.game.core.factory.WordFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.protobuf.SGMainProto;
import com.douqu.game.main.util.MsgUtils;
import com.douqu.game.main.server.GameServer;
import com.douqu.game.main.server.ServerManager;
import com.douqu.game.main.server.thread.MatchOfflineThread;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

/**
 * Created by bean on 2017/7/21.
 */
@Component
public class PlayerMsgChannel implements AMsgChannel {

    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void messageChannel(int code, PlayerController playerController, byte[] data) {

        switch (code)
        {
            case SGMainProto.E_MSG_ID.MsgID_Player_ArenaMatch_VALUE:
                match(playerController);
                break;
            case SGMainProto.E_MSG_ID.MsgID_Player_CancelArenaMatch_VALUE:
                break;
        }
    }


    /**
     * 匹配
     * @param playerController
     */
    private void match(PlayerController playerController)
    {
        logger.info("开始匹配-------------------------");

        if(playerController.isBattling())
            return;

        if(playerController.isMatching())
            return;

        if(!ServerManager.getInstance().isHaveServer(E_ServerType.TCP_BATTLE))
        {
            //战斗服务器没有开启，现在不能匹配
            logger.info("战斗服务器没有开启，现在不能匹配。。。。。。");
            playerController.sendAlert(SGCommonProto.E_NOTIFY_TYPE.NOTIFY_TYPE_WARN, WordFactory.getWord(WordFactory.BATTLE_SERVER_ERROR));
            return;
        }

        BattleInfo battleInfo = (BattleInfo) playerController.getPlayer().getExtInfo(BattleInfo.class);
        if(battleInfo.isTwoFail())
        {
            //匹配电脑
            logger.info("两连败，匹配电脑。。。。。。");
            GameServer.getInstance().startThread(new MatchOfflineThread(playerController));
        }
        else
        {
            logger.info("匹配池开始。。。。。。");
            GameServer.getInstance().getWorldManager().addMatch(playerController);
        }
    }
}
